package com.bix.util.blizfiles.dds;

/**
 * This object represents a DDS color without alpha information.
 * 
 *	@author		squid
 *
 *	@version	1.0.0
 */
class DDSColor {
	private int red;
	private int green;
	private int blue;

	/**
	 * Instantiate the color as block. 
	 */
	public DDSColor () {
		this.red = 0;
		this.green = 0;
		this.blue = 0;
	}
	
	public DDSColor (int red, int green, int blue) {
		this.setRed (red);
		this.setGreen (green);
		this.setBlue (blue);
	}
	
	/**
	 * Instantiate the color using an already existing color object.
	 * 
	 * @param color
	 */
	public DDSColor (DDSColor color) {
		this.setRed (color.getRed ());
		this.setGreen (color.getGreen ());
		this.setBlue (color.getBlue ());
	}
	
	/**
	 * Initializes a color object from a short type.  The format of the short is:
	 * <p>
	 * 		<code>rrrrrggggggbbbbb</code>
	 * <p>
	 * This is known as "565" format.  This constructor initializes the color so
	 * that each component fits into a single.
	 * 
	 * @param color565	The color in 565 bit format.
	 */
	public DDSColor (int color565) {
		//
		// Mask and shift each color so it fits into a single byte
		//
		this.setBlue ((color565 & 0x001F) << 3);
		this.setGreen (((color565 & 0x07E0) >> 3));
		this.setRed (((color565 & 0xF800) >> 8));
	}

	/**
	 * @return the red
	 */
	public int getRed () {
		return this.red;
	}
	
	/**
	 * Set red as an integer.
	 * 
	 * @param red
	 */
	public void setRed (int red) {
		this.red = red;
	}
	
	/**
	 * @return the green
	 */
	public int getGreen () {
		return this.green;
	}
	
	public void setGreen (int green) {
		this.green = green;
	}

	/**
	 * @return the blue
	 */
	public int getBlue () {
		return this.blue;
	}

	/**
	 * Set blue as an int.
	 * 
	 * @param blue
	 */
	public void setBlue (int blue) {
		this.blue = blue;
	}

	/**
	 * This method returns the color as a four byte integer where the format is:
	 * 
	 * rrrrrrrr gggggggg bbbbbbbb 00000000
	 * 
	 * The last byte is generally reserved for alpha but is set to zero as there
	 * is no alpha information in a normal color.
	 * 
	 * @return
	 */
	public int getAsInteger (){
		int	result = 0;
		
		result = result | this.getRed () << 16;
		result = result | (this.getGreen() << 8);
		result = result | (this.getBlue ());
		
		if (result < 0) {
			throw new RuntimeException ("Logic error.");
		}
		return result;
	}
	
	/**
	 * Return the color in 565 format.
	 * 
	 * @return	The color in packed 565 color format.
	 */
	public int getAs565 () {
		long	result = (this.getBlue ()  >> 3);
		result = result | (this.getGreen () << 3);
		result = result | (this.getRed ()   << 8 );
		return (int)(result & 0xFFFF);
	}
	
	public static void main (String args[]) {
		int	color1 = 53895;	// 0xD287
		int	color2 = 51880;	// 0xCAA8
		
		System.out.println (new DDSColor (color1).getAsInteger ());
		System.out.println (new DDSColor (color2).getAsInteger ());
		System.out.println (Integer.toHexString (new DDSColor (color1).getAsInteger ()));
		System.out.println (Integer.toHexString (new DDSColor (color2).getAsInteger ()));
		System.out.println (Integer.toHexString (new DDSColor (color1).getAs565 ()));
		System.out.println (Integer.toHexString (new DDSColor (color2).getAs565 ()));
		if (new DDSColor (color1).getAsInteger() <= new DDSColor (color2).getAsInteger()) {
			throw new RuntimeException ("INVALID!");
		}
	}
}

